﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Passwords.Models;
using System.Collections.ObjectModel;

namespace Passwords.Models
{
    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/PasswordsService")]
    public class PasswordContract : Entity
    {
        #region fields

        [DataMember(Name = "IsFavorite")]
        private bool isFavorite;

        [DataMember(Name="Name")]
        private string name;
        private ObservableChildCollection<CategoryContract> categories;
        private ObservableChildCollection<FieldContract> fields;

        #endregion
        #region props

        /// <summary>
        /// Gets whether the password has a template set.
        /// </summary>
        [IgnoreDataMember]
        public bool HasTemplate
        {
            get { return !string.IsNullOrEmpty(TemplateId); }
        }

        /// <summary>
        /// Gets or sets the template used for the default fields.
        /// </summary>
        [IgnoreDataMember]
        public CategoryContract Template
        {
            get
            {
                if (string.IsNullOrEmpty(TemplateId)) return null;
                var context = ViewModel;
                if (context == null) return null;
                return context.FindCategory(TemplateId);
            }
            set
            {
                if (value != Template)
                {
                    TemplateId = value != null ? value.Id : null;
                    OnPropertyChanged("Template");
                }
            }
        }


        public bool IsNew
        {
            get { return Guid == Guid.Empty; }
        }

        [DataMember]
        public Guid Guid { get; set; }

        [DataMember(Name = "CategoryType")]
        public string TemplateId { get; set; }

        [DataMember]
        public bool IsSynced { get; set; }

        public string Name
        {
            get { return name; }
            set
            {
                //if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("Name");
                if (name != value) name = value;
                OnPropertyChanged("Name");
            }
        }

        public bool IsFavorite
        {
            get { return isFavorite; }
            set
            {
                if (isFavorite != value)
                {
                    isFavorite = value;
                    OnPropertyChanged("IsFavorite");
                }
            }
        }

        [IgnoreDataMember]
        public ObservableChildCollection<CategoryContract> Categories
        {
            get
            {
                if (categories == null)
                {
                    categories = new ObservableChildCollection<CategoryContract>(this);
                }
                return categories;
            }
        }

        [DataMember]
        public DateTime Modified { get; set; }

        [IgnoreDataMember]
        public ObservableChildCollection<FieldContract> Fields
        {
            get
            {
                if (null == fields)
                {
                    fields = new ObservableChildCollection<FieldContract>(this);
                    LoadFields();

                }
                return fields;
            }
            set
            {
                fields = value;
            }
        }

        [DataMember]
        public byte[] EncryptedDetails { get; set; }

        /// <summary>
        /// Helper property only used when read/save/sync data.
        /// </summary>
        [DataMember(Name = "Categories")]
        public string[] CategoryIds { get; set; }

        //[IgnoreDataMember]
        //public byte[] EncryptedDetails { get; set; }

        #endregion
        #region methods

        public override string ToString()
        {
            return Name + ",  id=" + Guid.ToString();
        }

        public void LoadFields()
        {
            if (ViewModel != null)
            {
                var f = Fields;
                f.Clear();
                var array = ViewModel.Context.ReadPasswordFields(this.Guid.ToString());
                f.QuietMode = true;
                if (array != null)
                {
                    foreach (var field in array)
                    {
                        f.Add(field);
                    }
                }
                f.QuietMode = false;
            }
        }

        #endregion

    }
}
